package cn.lin.controller;

import cn.lin.service.FileService;
import cn.lin.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author cn.Lin
 * @create 2024-12-03-14:41 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 文件预览 通过目录id查询其对应的图片路径
     * @param catalogId 目录id */
    @GetMapping("/preview")
    public Object preview(@RequestParam("id") Integer catalogId){
        List<String> previewList = fileService.findFilePhotoPathById(catalogId);

        if (previewList != null){
            return Result.ok("获取成功",previewList);
        }
        return Result.error("获取失败");
    }

    @PostMapping("/upload")
    public Object uploadFile(@RequestParam("file") MultipartFile uploadFile,
                             @RequestParam("pid") int pid,
                             @RequestParam("classId") int classId) {

        boolean flag = fileService.addFilePhotoPath(uploadFile, pid,classId);

        if (flag) {
            return Result.ok("添加成功");
        }

        return Result.error("添加失败");
    }

}
